package com.ashlikun.wheelview_rv

import android.view.GestureDetector
import android.view.MotionEvent;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 作者　　: 李坤
 * 创建时间: 2024/7/25　13:06
 * 邮箱　　：496546144@qq.com
 *
 * 功能介绍：
 */
open class OnItemClickListener(
    private val recyclerView: RecyclerView,
    private val onLongClick: ((viewHolder: RecyclerView.ViewHolder, e: MotionEvent) -> Unit)? = null,
    private val onClick: ((viewHolder: RecyclerView.ViewHolder, e: MotionEvent) -> Unit)? = null
) :
    RecyclerView.OnItemTouchListener {
    val gestureDetector by lazy {
        GestureDetector(recyclerView.context, object : GestureDetector.SimpleOnGestureListener() {
            override fun onSingleTapUp(e: MotionEvent): Boolean {
                val touchView = recyclerView.findChildViewUnder(e.x, e.y)
                if (touchView != null) {
                    val viewHolder = recyclerView.getChildViewHolder(touchView)
                    onClick?.invoke(viewHolder, e)
                }
                return true
            }

            override fun onLongPress(e: MotionEvent) {
                super.onLongPress(e)
                val touchView = recyclerView.findChildViewUnder(e.x, e.y)
                if (touchView != null) {
                    val viewHolder = recyclerView.getChildViewHolder(touchView)
                    onLongClick?.invoke(viewHolder, e)
                }
            }
        })
    }

    interface OnItemClickListener {
        fun onItemClick(viewHolder: RecyclerView.ViewHolder, e: MotionEvent)
        fun onItemLongClick(viewHolder: RecyclerView.ViewHolder, e: MotionEvent)
    }

    override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
        gestureDetector.onTouchEvent(e)
        return false
    }

    override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {
        // 在此处处理触摸事件
    }

    override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
        // 当父视图请求RecyclerView不应拦截触摸事件时，会调用此方法
    }

}